package org.sgx.gapigui.client.ui.actions.pagespeed;

import org.sgx.gapi.client.apis.pagespeed.PageSpeedGetRequest;
import org.sgx.gapi.client.apis.pagespeed.PageSpeedResult;
import org.sgx.gapi.client.client.GAPICallback;
import org.sgx.gapigui.client.ui.editableModels.pagespeed.PageStats;
import org.sgx.gwteditors.client.impl1.complex.PropertyHaverEditor2;
import org.sgx.jsutil.client.JsNumber;
import org.sgx.jsutil.client.JsUtil;
import org.sgx.raphael4gwt.graphael.GPaper;
import org.sgx.raphael4gwt.graphael.GRaphaelLoader;
import org.sgx.raphael4gwt.graphael.pie.PieOpts;
import org.sgx.raphael4gwt.raphael.PaperWidget;
import org.sgx.raphael4gwt.raphael.Raphael;
import org.sgx.raphael4gwt.raphael.RaphaelLoader.Listener;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class PageSpeedCharts extends Composite {

	private static PageSpeedChartsUiBinder uiBinder = GWT.create(PageSpeedChartsUiBinder.class);

	interface PageSpeedChartsUiBinder extends UiBinder<Widget, PageSpeedCharts> {
	}

	@UiField TextBox urlText; 
	@UiField Button getPageSpeedButton; 
	@UiField  VerticalPanel results; 
	@UiField Element resourcesChart, bytesChart;  
	
	public PageSpeedCharts() {
		initWidget(uiBinder.createAndBindUi(this));
		getPageSpeedButton.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				doPageSpeed();
			}
		}); 		
//		init();
	}


	protected void doPageSpeed() {		
		new PageSpeedGetRequest(urlText.getText()).execute(
				new GAPICallback<PageSpeedResult>() {			
			@Override
			public void call(final PageSpeedResult resp) {
				if(resp.error()!=null) 
					Window.alert("PageSpeedGetRequest ERROR: "+JsUtil.dumpObj(resp.error(), true));
				else  {
					GRaphaelLoader.loadGRaphael(new Listener() {						
						@Override
						public void loaded(Exception error) {
							if(error==null)
								doShowStats(resp);	
							else
								Window.alert("raphael4gwt load error: "+JsUtil.dumpObj(resp.error(), true)); 
						}
					}); 						
				}
			}
		});	
	}


	protected void doShowStats(PageSpeedResult resp) {
		

		PageStats editable = PageStats.from(resp.pageStats()); 
		
		
		int[] resources = {editable.getCssResponseBytes()};
		String []resourceNames ={};
		
		int bytesTotal = editable.getTotalRequestBytes(); 
		int[] bytes = {
				editable.getCssResponseBytes(), 
				editable.getHtmlResponseBytes(), 
				editable.getImageResponseBytes(), 
				editable.getJavascriptResponseBytes(),
				editable.getOtherResponseBytes()
				};
		String[] byteNames = {
				"CSS Response Bytes "+ JsNumber.from( ((double)editable.getCssResponseBytes()) / ((double)bytesTotal)).toFixed(2)+"%", 
				"HTML Response Bytes "+ ( ((double)editable.getHtmlResponseBytes()) / ((double)bytesTotal))+"%", 
				"Image Response Bytes "+ ( ((double)editable.getImageResponseBytes()) / ((double)bytesTotal))+"%", 
				"JavaScript Response bytes "+ ( ((double)editable.getJavascriptResponseBytes()) / ((double)bytesTotal))+"%",
				"Other Resources bytes "+ ( ((double)editable.getOtherResponseBytes()) / ((double)bytesTotal))+"%"
				}; 
				
		GPaper bytesChartPaper = Raphael.paper(bytesChart,600,600).cast();
		

		PieOpts bytesChartOpts = PieOpts.create()
				.legend(byteNames)
				.legendpos("east"); 
		bytesChartPaper.piechart(100, 100, 100, bytes, bytesChartOpts);
		
		results.clear(); 
		PropertyHaverEditor2<PageStats> ed1 = new PropertyHaverEditor2<PageStats>(); 
		ed1.load(editable); 
		results.add(ed1.getWidget()); 
		
//		SimplePanel sp = new SimplePanel();
//		results.add(sp); 
//		paperWidget = paperWidget.getPaper();
//		GPaper paper = Raphael.paper(sp,600,600).cast();
//        PieOpts opts = PieOpts.create(); 

//		Window.alert("about to show chart: "+paper); 
//        paper.piechart(200f, 200, 100, new double[]{10, 40, 70, 90}, PieOpts.create());
	}


//	private void init() {
//		GRaphaelLoader.loadGRaphael(new Listener() {			
//			@Override
//			public void loaded(Exception error) {
//				if(error == null) {
//					showCharts(); 
//				}
//				else {
//					Window.alert("ERROR loading graphael : "+error);
//					error.printStackTrace(); 
//				}
//			}
//		}); 
//	}

	private void showCharts() {
        GPaper paper = Raphael.paper(0,0,600,600).cast();
        PieOpts opts = PieOpts.create(); 
        paper.piechart(200f, 200, 100, new double[]{10, 40, 70, 90}, opts);
	}

	
}
